﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class create_city : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

        ctp_lib.check_login(this.Page, "create_city.aspx");

        //this.Label3.Visible = false;
        this.Label4.Visible = false;

        if (!this.IsPostBack)
        {
            //ctpDataContext db=new ctpDataContext();
            //var user_lang=from p in db.user_languages
            //              join l in db.languages on p.language_no equals l.no
            //              where p.userno==(int)Session["userno"] && l.language_no==(int)Session["now_user_lang_no"]
            //              select new 
            //              {
            //                  p.language_no,
            //                  l.title
                              
            //              };

            //this.DropDownList2.DataSource = user_lang;
            //this.DropDownList2.DataTextField = "title";
            //this.DropDownList2.DataValueField = "language_no";
            //this.DataBind();

            if (Request["title"] != null) this.TextBox1.Text = Request["title"];

            this.RadioButtonList1.Items.Add(new ListItem("other language","-1"));

           // check_is_eng();
        }

    }

    //private void check_is_eng()
    //{
    //    this.Panel1.Visible = false;
    //    return;

    //    if (this.DropDownList2.SelectedValue != "0")
    //    {
    //        this.Panel1.Visible = true;
    //    }
    //    else
    //    {
    //        this.Panel1.Visible = false;
    //    }
    //}


    protected void Button1_Click(object sender, EventArgs e)
    {

        ctpDataContext db = new ctpDataContext();

        //if (this.DropDownList2.SelectedValue != "0")
        //{
        //    if (this.TextBox3.Text.Trim() == "")
        //    {
        //        this.Label3.Visible = true;
        //        return;
        //    }
        //}


        if (db.ct_profiles.Count(p => p.title == this.TextBox1.Text && p.language_no == 0) != 0)
        {
            this.Label4.Visible = true;
            return;
        }
        if(IsCityExisted(this.TextBox1.Text))
        {
           this.Label4.Visible = true;
            return;
        }
        
        ct_profile myct = new ct_profile();
        //ct_profile myct1 = new ct_profile();
        
        //int newctno = db.ct_profiles.Count(p => p.language_no == 0);

        int newctno = db.ct_profiles.Max(p => p.no) + 1;

        DatabaseUtil dbUtil = new DatabaseUtil();
        Hashtable lang_table = dbUtil.getLangTable();


        myct.title = this.TextBox1.Text.ToLower();
        myct.country = this.TextBox2.Text.ToLower();

        //dbUtil.writeTranslatedTxt(DatabaseUtil.fromTables.


        myct.continent = int.Parse(this.DropDownList1.SelectedItem.Value);
        myct.no = newctno;
        myct.language_no = (int)Session["now_user_lang_no"];
        myct.create_user_no = (int)Session["userno"];
        //myct.language_no=int.Parse(this.DropDownList2.SelectedValue);
        myct.speak_language_no = int.Parse(this.RadioButtonList1.SelectedValue);
        myct.add_time = DateTime.Now;

        myct.main_pic_fn = "coming.gif";


        db.ct_profiles.InsertOnSubmit(myct);
        db.SubmitChanges();
        dbUtil.writeTranslatedTxt((int)DatabaseUtil.fromTables.ct_profile_title, myct.no, myct.title, CitiportUser.LangCode);

        //if (this.DropDownList2.SelectedValue != "0")
        //{
        //    myct1.title = this.TextBox3.Text.ToLower();
        //    myct1.country = this.TextBox4.Text.ToLower();
        //    myct1.continent = int.Parse(this.DropDownList1.SelectedItem.Value);
        //    myct1.no = newctno;
        //    myct1.language_no=0;
        //    myct1.speak_language_no = int.Parse(this.RadioButtonList1.SelectedValue);


        //    db.ct_profiles.InsertOnSubmit(myct1);
        //    db.SubmitChanges();
        //}
   
        
 

        Response.Redirect("index.aspx");

    }

    private bool IsCityExisted(String city)
    {
        if (city == null)
            return false;
        ctpDataContext dbt = new ctpDataContext();
        city = city.Trim().ToLower();
        int count = (from p in dbt.ct_language_texts
                     where p.text.Contains(city)&& p.from_table_no == 7
                     select p).Count();
        return (count > 0);
    }
}
